using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using TMPro;

namespace MoviePlay
{
    public class PlayMp4Video : MonoBehaviour
    {
        #region 参数
        //定义参数获取video组件和RawImage组件
        VideoPlayer videoPlayer;
        public RawImage rawImage;
        public TextMeshProUGUI textMeshPro;

        public string movieUrl;

      
        bool isPlay = false;
        bool isPause =false;
        //声音播放组件
        private AudioSource thisAudioSource;
        //是否静音
        bool isMute = false;

        #endregion
        // Use this for initialization
        void Start()
        {
            BeginString();
        }

        // Update is called once per frame
        void Update()
        {
            PlayMovie();
        }

        #region 私有方法
        /// <summary>
        /// 初始化数据
        /// </summary>
        void BeginString()
        {
            videoPlayer = GameObject.Find("moviePlayManager").GetComponent<VideoPlayer>();
            if(string.IsNullOrEmpty(movieUrl))
            {
                movieUrl = Application.streamingAssetsPath + "/1089_1694596919.mp4";
            }
            isPlay = false;
            isPause = false;
        }

        /// <summary>
        /// 播放视频函数
        /// </summary>
        void PlayMovie()
        {
            //如果videoPlayer没有对应的视频texture，则返回
            if (videoPlayer.texture == null)
            {
                return;
            }
            //把VideoPlay的视频渲染到UGUI的RawImage上
            rawImage.texture = videoPlayer.texture;
            if (isPlay)
            {
                if (videoPlayer.frame == (long)videoPlayer.frameCount)
                {
                    Debug.Log("视频播放完毕动作！");
                }
            }
        }

        /// <summary>
        /// 播放视频0逻辑
        /// </summary>
        public void ToPlayThis0()
        {
            videoPlayer.url = movieUrl;
            StartCoroutine(ToPlay0());
        }

        /// <summary>
        /// 播放视频0的协程
        /// </summary>
        /// <returns></returns>
        IEnumerator ToPlay0()
        {
            yield return new WaitForSeconds(0.1f);
            BeginPlay();
        }

        /// <summary>
        /// 开始播放视频
        /// </summary>
        void BeginPlay()
        {
            if (!isPlay)
            {
                videoPlayer.Play();
                isPlay = true;
            }
            isPause = false;
        }

        /// <summary>
        /// 停止播放视频
        /// </summary>
        public void StopPlay()
        {
            videoPlayer.Stop();
            isPlay = false;
            isPause = false;
        }

        /// <summary>
        /// 暂停播放视频逻辑
        /// </summary>
        public void PauseThis()
        {
            if (isPause)
            {
                videoPlayer.Play();
                isPause = false;
                textMeshPro.text = "pause";
            }
            else
            {
                videoPlayer.Pause();
                isPause = true;
                textMeshPro.text = "resume";
            }
        }

        #endregion
    }
}


